/*  
 * i-OSGi - Tunable Bundle Isolation for OSGi
 * Copyright (C) 2011  Sven Schulz
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.iosgi.outpost.operations;

import java.io.File;
import java.io.Serializable;
import java.util.List;

import org.iosgi.outpost.Operation;
import org.iosgi.util.io.Streams;

/**
 * @author Sven Schulz
 */
public class Exec implements Operation<Integer>, Serializable {

	private static final long serialVersionUID = 1520027915858678197L;

	private final File workDir;
	private final List<String> command;
	private final boolean block;
	private final File out, err;

	public Exec(File workDir, List<String> command) {
		this(workDir, command, true, null, null);
	}

	public Exec(File workDir, List<String> command, boolean block, File out,
			File err) {
		this.workDir = workDir;
		this.command = command;
		this.block = block;
		this.out = out;
		this.err = err;
	}

	@Override
	public Integer perform() throws Exception {
		ProcessBuilder b = new ProcessBuilder();
		b.command(command);
		b.directory(workDir);
		Process p = b.start();
		if (out != null)
			Streams.drain(p.getInputStream(), out);
		if (err != null)
			Streams.drain(p.getErrorStream(), err);
		return block ? p.waitFor() : 0;
	}

}
